package com.zhixin.fans.ks.ui.main.person;


import android.app.Fragment;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.api.CoupleApi;
import com.zhixin.fans.ks.api.res.UserInfo;
import com.zhixin.fans.ks.manager.UserInfoManager;
import com.zhixin.fans.ks.ui.base.BaseDaggerFragment;
import com.zhixin.fans.ks.ui.charge.ChargeCoinActivity;
import com.zhixin.fans.ks.ui.charge.ChargeVipActivity;
import com.zhixin.fans.ks.ui.charge.FirstChargeActivity;
import com.zhixin.fans.ks.ui.history.OrderRecordActivity;
import com.zhixin.fans.ks.ui.login.LoginActivity;
import com.zhixin.fans.ks.ui.main.MainActivity;
import com.zhixin.fans.ks.ui.sign.DailySignActivity;
import com.zhixin.fans.ks.ui.user.UserDetailActivity;

import org.eenie.common.views.QMUIWindowInsetLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class PersonalFragment extends BaseDaggerFragment implements Observer<UserInfo> {
    @BindView(R.id.tv_sign)
    TextView mTvSign;
    @BindView(R.id.fragment_container)
    QMUIWindowInsetLinearLayout mFragmentContainer;
    @BindView(R.id.lay_charge_first)
    LinearLayout mLayChargeFirst;
    @BindView(R.id.lay_charge_coin)
    LinearLayout mLayChargeCoin;

    @BindView(R.id.lay_charge_history)
    LinearLayout mLayChargeHistory;
    @BindView(R.id.lay_charge_service)
    LinearLayout mLayChargeService;

    UserInfoManager mUserInfoManager;
    @BindView(R.id.img_user_header)
    QMUIRadiusImageView mImgUserHeader;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_user_id)
    TextView mTvUserId;
    @BindView(R.id.lay_user_info)
    LinearLayout mLayUserInfo;
    @BindView(R.id.tv_fb_count)
    TextView mTvFbCount;
    @BindView(R.id.btn_buy_fb)
    QMUIRoundButton mBtnBuyFb;


    CoupleApi mCoupleApi;

    Unbinder unbinder;


    public PersonalFragment(String title) {
        super(title);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_personal;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mUserInfoManager = getCoupleAppComponent().getUserInfoManager();
        mCoupleApi = getApplicationComponent().getApiManager().create(CoupleApi.class);
    }

    @Override
    public void initUI(View rootView) {
        unbinder = ButterKnife.bind(this, rootView);
        mUserInfoManager.observe(getViewLifecycleOwner(), this);
        mUserInfoManager.update();
    }

    @OnClick(R.id.tv_sign)
    public void onViewClicked() {

        startActivity(new Intent(mContext, DailySignActivity.class));

    }

    @OnClick({R.id.lay_charge_first, R.id.lay_charge_coin, R.id.lay_charge_history, R.id.lay_charge_service, R.id.lay_user_info, R.id.btn_buy_fb, R.id.lay_fetch_coin,R.id.lay_fetch_vip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lay_charge_first:
                startActivity(new Intent(mContext, FirstChargeActivity.class));

                break;
            case R.id.lay_charge_coin:
            case R.id.btn_buy_fb:
                startActivity(new Intent(mContext, ChargeCoinActivity.class));

                break;
            case R.id.lay_fetch_vip:
                startActivity(new Intent(mContext, ChargeVipActivity.class));

                break;
            case R.id.lay_charge_history:
                startActivity(new Intent(mContext, OrderRecordActivity.class));
                break;
            case R.id.lay_user_info:
                if (mUserInfoManager.isLogin()) {
                    startActivity(new Intent(mContext, UserDetailActivity.class));
                } else {
                    startActivity(new Intent(mContext, LoginActivity.class));
                }
                break;
            case R.id.lay_charge_service:
                ((MainActivity) getActivity()).onTabClick(1);
                break;
        }
    }

    @Override
    public void onChanged(UserInfo userInfo) {
        if (userInfo != null && userInfo.getU_id() > 0) {
            Glide.with(mContext).asBitmap().load(userInfo.getU_headPicture()).into(mImgUserHeader);
            mTvUserName.setText(userInfo.getU_nickName());
            mTvUserId.setText("ID:" + String.valueOf(userInfo.getU_id()));

            mTvFbCount.setText(String.format("我的粉币：%s", userInfo.getU_usermoney()));


        } else {
            Glide.with(mContext).asBitmap().load(R.mipmap.ic_model_qq).into(mImgUserHeader);
            mTvUserName.setText("未登录");
            mTvUserId.setText("ID:-------");
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
